package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.mapper.UserMapper;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Leave;
import cn.wolfcode.car.business.mapper.LeaveMapper;
import cn.wolfcode.car.business.query.LeaveQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.ILeaveService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class LeaveServiceImpl implements ILeaveService {

    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public TablePageInfo<Leave> query(LeaveQuery qo) {

        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Leave>(leaveMapper.selectForList(qo));
    }


    @Override
    public void save(Leave leave, Long bpmnInfoId) {

        //修改服务状态，修改为审核中
        // leave.setStatus(leave.AUDITSTATUS_AUDITING + "");
        leave.setStatus("0");
        leaveMapper.insert(leave);
        // 获取审核人信息
        User user = userMapper.selectByPrimaryKey(leave.getAutitor().getId());
        System.out.println(user);
        Map<String, Object> map = new HashMap<>();
        map.put("s1", user.getUserName());
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId); // 获取流程定义
        //启动流程  businessKey为String类型
        // runtimeService.startProcessInstanceByKey("leaveProcess", businessKey, variables);
        runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), leave.getId().toString(), map);
    }

    @Override
    public Leave get(Long id) {
        return leaveMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Leave leave) {

        leaveMapper.updateByPrimaryKey(leave);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            leaveMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Leave> list() {
        return leaveMapper.selectAll();
    }



}
